package com.ssyt.store.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssyt.store.common.BaseController;
import com.ssyt.store.common.PageModel;
import com.ssyt.store.common.util.PageConstants;
import com.ssyt.store.common.util.StringUtil;
import com.ssyt.store.sys.entity.StoreProduct;
import com.ssyt.store.sys.service.ProductService;

/**
 * 产品控制层
 *
 */
@Controller
@RequestMapping("/shopping/product")
public class ProductController extends BaseController{
	
	private String bookname = "";
	private Integer bookflag = 0;
	private Integer bookprice = 0;
	
	@Autowired
	private ProductService productService;
	private static Logger log = LogManager.getLogger(StoreProduct.class);

	@RequestMapping("/showAllProducts")
	public  String showAllProducts(Integer pageNo,Integer pageSize,String loginFlag,Model model){
		if(pageNo == null){
			pageNo = PageConstants.PAGE_NO;
		}
		if(pageSize == null){
			pageSize = PageConstants.PAGE_SIZE;
		}
		PageModel<StoreProduct> showPageAllProducts = productService.showAllProducts(pageNo,pageSize);
		model.addAttribute("showPageAllProducts",showPageAllProducts);
		model.addAttribute("loginFlag", loginFlag);
		return "store/index";
	}
	
	@RequestMapping("/AllProducts")
	@ResponseBody
	public  List<StoreProduct> AllProducts(Integer pageNo,Integer pageSize){
		if(pageNo == null){
			pageNo= PageConstants.PAGE_NO;
		}else{
			pageNo = pageNo/10;
		}
		if(pageSize == null){
			pageSize = 7;
		}
		return (productService.showAllProducts(pageNo,pageSize)).getList();
	}
	
	@RequestMapping("/listAllProducts")
	public  String listAllProducts(Integer pageNo,Integer pageSize,String name,Integer flag,Integer price,Model model){
		if(pageNo == null){
			pageNo = PageConstants.PAGE_NO;
		}
		if(pageSize == null){
			pageSize = PageConstants.PAGE_SIZE;
		}
		if(!StringUtil.isNotEmpty(name)){
			name = "";
		}
		if(flag == null){
			flag = 0;
		}
		if(price == null){
			price = 0;
		}
		this.bookname = name;
		this.bookflag = flag;
		this.bookprice = bookprice;
		PageModel<StoreProduct> showPageAllProducts = productService.ListAllProducts(pageNo, pageSize, name, flag, price);
		model.addAttribute("showPageAllProducts",showPageAllProducts);
		return "store/productlist";
	}
	
	
	@RequestMapping("/queryAllProduct")
	@ResponseBody
	public  List<StoreProduct> queryAllProducts(Integer pageNo,Integer pageSize){
		if(pageNo == null){
			pageNo= PageConstants.PAGE_NO;
		}else{
			pageNo = pageNo/10;
		}
		if(pageSize == null){
			pageSize = 7;
		}
		return (productService.ListAllProducts(pageNo, pageSize, this.bookname, this.bookflag, this.bookprice)).getList();
	}
	
	
	@RequestMapping("/productInfo")
	public String productInfo(String productId,Model model){
		StoreProduct product = productService.productInfo(productId);
		model.addAttribute("product",product);
		return "store/info";
	}
}
